package com.liming.product.excel;

import cn.hutool.core.lang.Assert;
import com.liming.common.constant.SystemConstants;
import com.liming.common.excel.AbstractExcelListener;
import com.liming.common.exception.BusinessException;
import com.liming.common.pojo.dto.product.ProductImportDTO;
import com.liming.common.pojo.dto.stock.InitStockDTO;
import com.liming.common.pojo.entity.product.Product;
import com.liming.common.util.BeanUtils;
import com.liming.common.util.ThreadLocalUtils;
import com.liming.product.service.BrandMircoService;
import com.liming.product.service.CategoryMircoService;
import com.liming.product.service.impl.ProductMircoServiceImpl;
import com.liming.stock.client.StockClient;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import static com.liming.common.constant.DBConstants.PRODUCT_ZERO_QUANTITY;
import static com.liming.common.constant.DBConstants.WAREHOUSE_DEFAULT_ID;

/**
 * @author liming
 * @date 2025/5/15
 */
public class ProductExcelListener extends AbstractExcelListener<ProductImportDTO, Product> {

    private final ProductMircoServiceImpl productMircoService;
    private final BrandMircoService brandMircoService;
    private final CategoryMircoService categoryMircoService;
    private final StockClient stockClient;

    public ProductExcelListener(ProductMircoServiceImpl productMircoService, BrandMircoService brandMircoService,
                                CategoryMircoService categoryMircoService, StockClient stockClient) {
        super(true);
        this.productMircoService = productMircoService;
        this.brandMircoService = brandMircoService;
        this.categoryMircoService = categoryMircoService;
        this.stockClient = stockClient;
    }

    @Override
    protected List<Product> convertData() {
        List<ProductImportDTO> sourceData = getSourceData();
        List<Product> convertData = BeanUtils.toBean(sourceData, Product.class);
        for (Product product : convertData) {
            product.setPrice(product.getPrice() * 100);
            product.setOriginalPrice(product.getOriginalPrice() * 100);
            product.setOnShelf(0);
            // 商品新添加时销量为0
            product.setSalesCount(PRODUCT_ZERO_QUANTITY);
            product.setBrandId(brandMircoService.getByName(product.getBrandName()).getId());
            product.setCategoryId(categoryMircoService.getByName(product.getCategoryName()).getId());
            product.setMainImageUrl("Don't have image");
        }
        return convertData;
    }

    @Override
    protected void saveData() {
        List<Product> productList = this.getConvertData();
        Assert.isTrue(productMircoService.saveBatch(productList), () -> new BusinessException("商品保存失败"));
        String id = ThreadLocalUtils.get(SystemConstants.ID);
        for (Product product : productList) {
            CompletableFuture.runAsync(() -> {
                InitStockDTO stockDTO = new InitStockDTO();
                stockDTO.setOperatorId(id);
                stockDTO.setProductId(product.getId());
                stockDTO.setWarehouseId(WAREHOUSE_DEFAULT_ID);
                Assert.isTrue(stockClient.initStock(stockDTO), () -> new BusinessException("库存保存失败"));
            });
        }
    }
}
